View Javadoc
1 package jrre; 2 3 import jrre.event.*; 4 import jrre.instructionset.Instruction; 5 6 import jrre.types.*; 7 import jrre.gui.*; 8 import jrre.classloader.ClassLoader; 9 import jrre.classloader.classfile.pool_entries.*; 10 11 import java.awt.event.*; 12 import java.awt.*; 13 import java.lang.*; 14 15 import java.beans.*; 16 /*** 17 * The Java Reasearch Runtime Enviroments virtual machine implementation. This 18 * class manages the interactions between the objectArea, classloader, and 19 * the instruction and operand stacks. 20 * 21 * 22 * @author Christopher Ellsworth (chris@chrisellsworth.com 23 * @author Clerance Alston (massclax@hotmail.com) 24 */ 25 public class JRRE implements ActionListener,Runnable{ 26 27 28 private static ActionListener actionListenerList; 29 30 //private PropertyChangeSupport propertyChange = new PropertyChangeSupport(this); 31 private static PropertyChangeSupport propertyChange; 32 33 /*** 34 * The JRRE's runtime object area. This contains all 35 * of the objects that have been instanciated with the 36 * <code>new</code> operation. 37 * 38 * @see jrre.ObjectArea 39 */ 40 private ObjectArea objectArea; 41 42 private NativeMethodArea nativeMethodArea; 43 private NativeStackFrame nativeStackPointer; 44 45 private static jrre.api.java.lang.Class rootClass; 46 47 /*** 48 * Gets the RootClass. 49 */ 50 public static jrre.api.java.lang.Class getRootClass(){ 51 return rootClass; 52 } 53 54 /*** 55 * Sets the RootClass. 56 * @param RootClass The value to set it to. 57 */ 58 public static void setRootClass(jrre.api.java.lang.Class rootClass){ 59 rootClass = rootClass; 60 } 61 62 //private Stack stack; 63 private Stack framePointer; 64 65 private String classPath; 66 /*** 67 * The bootstrap classloader to begin loading classes with. 68 * @see jrre.classloader.ClassLoader 69 **/ 70 private ClassLoader classLoader; 71 72 /*** 73 * GUI Control bar. 74 */ 75 private ControlGui controlGui; 76 77 /*** 78 * A boolean to detrumine weather the Graphical User Interface 79 * in enabled or not. This is set with the -gui option from the 80 * command line. 81 **/ 82 private static boolean gui; 83 84 private static boolean runAtLoad = false; 85 86 /*** 87 * A boolean to deturmine if verbose mode is enabled. When 88 * enabled, JRRE operations are printed on standard output. This 89 * is set with the -v option from the command line. 90 **/ 91 private static boolean verbose; 92 93 private static VMEventDispatcher eventDispatcher; 94 95 /*** 96 * Initialises the runtime enviroment, loads the class specified, and 97 * begin's execution of its <code>init</code> method. 98 * 99 * @param fileName 100 */ 101 public JRRE(String fileName){ 102 103 eventDispatcher = new VMEventDispatcher(); 104 propertyChange = new PropertyChangeSupport(this); 105 106 objectArea = new ObjectArea(); 107 nativeMethodArea = new NativeMethodArea(); 108 nativeStackPointer = new NativeStackFrame(); 109 framePointer = new Stack(); 110 111 //Stack.addPropertyChangeListener(this); 112 113 classPath = "target/classes;target/test-classes;api"; 114 115 //if(gui) console = new Console(); 116 117 ClassLoader.setClassPath(classPath); 118 //rootClass = ClassLoader.loadClass(fileName); 119 120 //System.out.println(fileName); 121 122 rootClass = MethodArea.getClass(fileName); 123 124 MethodArea.doneLoading(); 125 126 //if(gui) console.reportMessage(ClassLoader.getMessages().toString()); 127 //else if(verbose) System.out.println(ClassLoader.getMessages().toString()); 128 129 if(guiOn()){ 130 controlGui = new ControlGui(); 131 controlGui.addActionListener(this); 132 } 133 134 load(rootClass); 135 136 } 137 138 public static void processEvent(VMEvent event){ 139 140 eventDispatcher.fireEvent(event); 141 } 142 143 /*** 144 * @task change to some PropertyChangeEvent object. 145 */ 146 /* 147 public static void firePropertyChange(VMEvent property){ 148 149 //propertyChange.firePropertyChange(property); 150 } 151 */ 152 153 /*** 154 * Gets the Gui. 155 * @deprecated 156 */ 157 public static boolean getGui(){ 158 return gui; 159 } 160 161 public static boolean guiOn(){ 162 return gui; 163 } 164 165 public static boolean getRunAtLoad(){ 166 return runAtLoad; 167 } 168 169 public static void setRunAtLoad(boolean runAtLoadSet){ 170 runAtLoad = runAtLoadSet; 171 } 172 173 /*** 174 * Sets the Gui. 175 * @param Gui The value to set it to. 176 */ 177 public static void setGui(boolean guiBool){ 178 gui = guiBool; 179 } 180 181 /*** 182 * Gets the Verbose mode. 183 */ 184 public static boolean getVerbose(){ 185 return verbose; 186 } 187 188 /*** 189 * Sets the Verbose mode. 190 * @param Verbose The value to set it to. 191 */ 192 public static void setVerbose(boolean verboseBool){ 193 verbose = verboseBool; 194 } 195 196 public void addEventListener(VMEventListener listener){ 197 198 eventDispatcher.addEventListener(listener); 199 } 200 201 /* 202 public void addPropertyChangeListener(PropertyChangeListener listener){ 203 204 propertyChange.addPropertyChangeListener(listener); 205 } 206 */ 207 208 /*** 209 * Handles action events fired by button presses and passes them 210 * to registered action listeners. 211 * @param Event fired. 212 */ 213 public void propertyChange(PropertyChangeEvent event){ 214 215 String action = event.getPropertyName(); 216 217 System.out.println("in propertyChange?? "+action); 218 } 219 220 /*** 221 * Handles action events fired by button presses and passes them 222 * to registered action listeners. 223 * @param Event fired. 224 */ 225 public void actionPerformed(ActionEvent event){ 226 227 String action = event.getActionCommand(); 228 229 //System.out.println(action); 230 231 if(action.equals("Run")) 232 beginExecution(); 233 else if(action.equals("Pause")) 234 pause(); 235 else if(action.equals("Halt")) 236 halt(); 237 else if(action.equals("Step")) 238 step(((ControlGui)event.getSource()).getStepCount()); 239 } 240 241 /*** 242 * 243 * 244 */ 245 public void run(){ 246 247 } 248 249 /*** 250 * Begin/resume execution at the next instruction. 251 */ 252 public static void beginExecution(){ 253 254 while(Stack.getStackPointer() != null){ 255 /* 256 try{ 257 .sleep(1000); 258 } 259 catch(InterruptedException e){ 260 System.out.println("Error in JRRE sleep"); 261 } 262 263 */ 264 if(!execute()) 265 break; 266 } 267 268 if(!guiOn() && getRunAtLoad()) 269 System.exit(0); 270 //halt(); 271 } 272 273 public static void pause(){ 274 275 } 276 277 /*** 278 * Halt execution. 279 */ 280 public static void halt(){ 281 //framePointer.clearStack(); 282 ObjectArea.clear(); 283 284 Stack.clearStack(); 285 //framePointer = new Stack(); 286 287 load(rootClass); 288 } 289 290 /*** 291 * Step to the next instruction. 292 */ 293 public static boolean step(int stepCount){ 294 295 for(int i=0;i < stepCount;i++){ 296 if(!execute()) 297 return false; 298 } 299 return true; 300 } 301 302 /*** 303 * Execute the top instruction. 304 */ 305 public static boolean execute(){ 306 307 // fetch 308 Instruction instruction = Stack.getNextInstruction(); 309 310 if(instruction == null) 311 return false; 312 313 if(JRRE.getVerbose()) 314 System.out.println(":: "+instruction); 315 316 // execute 317 instruction.execute(); 318 319 return true; 320 } 321 322 /*** 323 * Initialises the enviroment, loads the class specified and 324 * any classes it referanced, and begins execution of its 325 * <code>init</code> method. 326 */ 327 public static void load(jrre.api.java.lang.Class classToLoad){ 328 329 //stack = new Stack(); 330 331 CPClass classEntry = (CPClass)classToLoad.getSymbol(classToLoad.getProperties().getThisClass()); 332 String classDescriptor = (String) ((CPUTF8)classToLoad.getSymbol(classEntry.getNameIndex())).getValue(); 333 334 ObjectInstance thisObject = classToLoad.instanceOf(); 335 ObjectArea.addObject(classDescriptor+"<init class>", thisObject); 336 337 // get init method stack frame. 338 jrre.api.java.lang.reflect.MethodEntry mainMethodFrame = classToLoad.getMethod("main([Ljava/lang/String;)V"); 339 if(mainMethodFrame != null){ 340 341 if(JRRE.getVerbose()) 342 System.out.println("Pushing Main Method Stack Frame"); 343 // run main method. 344 Stack.pushInitial(mainMethodFrame.getStackFrame(), classToLoad.getFullyQualifiedName()+"::main([Ljava/lang/String;)"); 345 } 346 else { 347 StackFrame initMethodFrame = classToLoad.getMethod("<init>()V").getStackFrame(); 348 349 Stack.pushInitial(initMethodFrame, "<init>()V::"+classToLoad.getFullyQualifiedName()); 350 } 351 Stack.setLocalVariable(0, new ReferenceType(thisObject)); 352 353 // push init method on stacks. 354 355 // run. 356 if(getRunAtLoad()) 357 beginExecution(); 358 } 359 360 }

This page was automatically generated by Maven